package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.UserService;
import com.dongdongshop.util.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;

@Service
@org.springframework.stereotype.Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private TbUserMapper tbUserMapper;

    @Override
    public int insertUser(TbUser tbUser) {
        String salt = ShiroUtils.generateSalt(6);
        tbUser.setSalt(salt);
        tbUser.setPassword(ShiroUtils.encryptPassword("MD5",tbUser.getPassword(),salt,3));
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        tbUser.setStatus("Y");
        tbUser.setIsMobileCheck("1");
        tbUser.setIsEmailCheck("0");
        tbUserMapper.insertSelective(tbUser);
        logger.info("执行成功");
        return 1;
    }

    @Override
    public TbUser getUserByName(String username) {
        return tbUserMapper.getUserByName(username);
    }

    @Override
    public void updateUser(TbUser user) {
        tbUserMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public TbUser getUserByPhone(String phone) {
        return tbUserMapper.getUserByPhone(phone);
    }

    @Override
    public int findMyPassword(TbUser tbUser) {
        String salt = ShiroUtils.generateSalt(6);
        tbUser.setSalt(salt);
        tbUser.setPassword(ShiroUtils.encryptPassword("MD5",tbUser.getPassword(),salt,3));
        tbUserMapper.updateByPrimaryKeySelective(tbUser);
        return 1;
    }

}
